<?php
declare (strict_types = 1);
namespace app\admin\model\market\advert;

use app\common\logic\Attach;
use app\common\model\BaseModel;

/**
 * 定义广告图配置数据模型
 */
class AdvertLink extends BaseModel
{
    /**
     * 表名
     * @var string
     */
    protected $name = 'advert_link';

    /**
     * 设置自动写入时间
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    /**
     * 无需创建时间
     * @var boolean
     */
    protected $createTime = false;
    /**
     * 时间显示格式
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';

    /**
     * 填充字段
     * @var [type]
     */
    protected $append = ['link_type_text', 'link_data_type_text'];

    /**
     * 定义表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 文字跳转类型
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-26
     * @param    [type]                         $value [description]
     * @param    [type]                         $data  [description]
     * @return   [type]                                [description]
     */
    public function getLinkTypeTextAttr($value, $data)
    {
        $text = [
            0 => '纯图片',
            1 => '内容',
            2 => '链接',
        ];

        return $text[$data['link_type']];
    }

    /**
     * 设置类型
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-26
     * @param    [type]                         $value [description]
     * @param    [type]                         $data  [description]
     */
    public function setLinkDataTypeAttr($value, $data)
    {
        $type = '';
        switch ($value) {
            case '1':
                $type = 'video';
                break;
            case '2':
                $type = 'live';
                break;
            case '3':
                $type = 'live_small';
                break;
            case '4':
                $type = 'classes';
                break;
            default:
                $type = 'public';
                break;
        }

        return $type;
    }

    /**
     * 跳转的内容数据名称
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-26
     * @param    [type]                         $value [description]
     * @param    [type]                         $data  [description]
     * @return   [type]                                [description]
     */
    public function getLinkDataTypeTextAttr($value, $data)
    {
        $text = [
            'video'      => '点播',
            'live'       => '大班课',
            'live_small' => '小班课',
            'classes'    => '班级',
            'public'     => '公共',
        ];

        return $data['link_type'] == 1 ? $text[$data['link_data_type']] : '';
    }

    public function imageUrl()
    {
        return $this->belongsTo(Attach::class, 'image', 'id')->bind(['image_url' => 'fileurl']);
    }

    /**
     * 成功创建之后
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-10-14
     */
    public static function onAfterInsert()
    {
        self::cleanCache();
    }

    /**
     * 成功更新之后
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-10-14
     */
    public static function onAfterUpdate()
    {
        self::cleanCache();
    }

    /**
     * 成功删除之后
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-10-14
     */
    public static function onAfterDelete()
    {
        self::cleanCache();
    }

    /**
     * 清理缓存
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-10-14
     * @return [type]                      [description]
     */
    protected static function cleanCache()
    {
        $keys = self::getPlaceKeys();

        array_walk($keys, function ($val, $key) {
            cache("advert_".$val, null);
        });

        if (file_exists('home_pc.txt')) unlink('home_pc.txt');
        if (file_exists('home_ot.txt')) unlink('home_ot.txt');
    }

    /**
     * 获取key值
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-10-14
     * @return [type]                      [description]
     */
    protected static function getPlaceKeys()
    {
        return app(AdvertPlace::class)
            ->alias('p')
            ->join('advert a', 'a.place=p.id AND a.mhm_id='.request()->mhm_id)
            ->column('key');
    }
}
